package cc.rengu.igas.momp.core.service.trans;

import cc.rengu.igas.momp.common.constant.CommonConstant;
import cc.rengu.igas.momp.common.constant.DatabaseLabelConstant;
import cc.rengu.igas.momp.common.dao.*;
import cc.rengu.igas.momp.common.dao.impl.*;
import cc.rengu.igas.momp.common.entity.*;
import cc.rengu.igas.momp.common.enums.*;
import cc.rengu.igas.momp.common.util.JsonOperation;
import cc.rengu.igas.momp.common.util.MompDateUtil;
import cc.rengu.igas.momp.facade.bean.McMchntInfoBean;
import cc.rengu.igas.momp.facade.request.AddMchntForToClientMcRequest;
import cc.rengu.igas.momp.facade.response.AddMchntForToClientMcResponse;
import cc.rengu.oltp.service.base.RadpService;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.enums.ChannelEnum;
import cc.rengu.oltp.service.common.enums.OltpRpcdEnum;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.bean.PageInfo;
import cc.rengu.oltp.utility.bean.PageResult;
import cc.rengu.oltp.utility.util.*;
import cc.rengu.utility.base.StringUtil;
import cc.rengu.utility.dbs.Database;
import com.alibaba.fastjson.JSON;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 进行中营销活动添加商户功能实现
 *
 * @author Jinan Liu
 * @version 1.0.0
 * @date 2020/4/8 21:16
 */
public class UnderwayMcAddMchntService extends RadpService {

    /**
     * 最后更新操作员编号
     */
    private String lastOprId;
    /**
     * 英文表名
     */
    private String tableEName;
    /**
     * 中文表名
     */
    private String tableCName;
    /**
     * 活动状态
     */
    private String auditStatus;
    private String instId;
    private String mcNo;
    private String mcType;
    private String currentTime;
    private String txnDate;
    private String queryId;
    private List<McMchntInfoBean> mcMchntInfoBeanList = new ArrayList();
    private String transNo;

    @Override
    protected int bizInit(String s) {
        return CommonConstant.PROCESS_SUCCESS;
    }

    @Override
    protected int bizProcess(String s, int i) {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();

        try {

            AddMchntForToClientMcRequest addMchntForToClientMcRequest = new AddMchntForToClientMcRequest();
            ConvertUtil.convertOutput(addMchntForToClientMcRequest);
            queryId = addMchntForToClientMcRequest.getQueryId();
            txnDate = addMchntForToClientMcRequest.getHeader().getTransDate();
            if (null != addMchntForToClientMcRequest.getMchntInfoList() && addMchntForToClientMcRequest.getMchntInfoList().size() > 0) {
                mcMchntInfoBeanList = addMchntForToClientMcRequest.getMchntInfoList();
            }

            AddMchntForToClientMcResponse addMchntForToClientMcResponse = new AddMchntForToClientMcResponse();
            addMchntForToClientMcResponse.setHeader(addMchntForToClientMcRequest.getHeader());
            BizResponse<AddMchntForToClientMcResponse> bizResponse = new BizResponse<>();
            bizResponse.setResult(addMchntForToClientMcResponse);

            /* 报文检查 */
            messageValidation(addMchntForToClientMcRequest);

            /* 调用处理流程 */
            BizResponse<AddMchntForToClientMcResponse> bizResponseNew = underwayMcAddMchnt(addMchntForToClientMcRequest, addMchntForToClientMcResponse);

            ConvertUtil.convertInput(bizResponseNew.getResult());

        } catch (BizException e) {
            try {
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_CODE, e.getCode());
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_DESC, e.getMessage());
                xmlTreeUtil.setXmlTreeStringValue(CommonConstant.KEY_RESPDESC, e.getMessage());
            } catch (Exception e1) {
                rglog.error(StringUtil.ExceptionToString(e1));
            }
        } catch (Exception e) {
            rglog.error(StringUtil.ExceptionToString(e));
            try {
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_CODE, OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespCode());
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_DESC, OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespDesc());
                xmlTreeUtil.setXmlTreeStringValue(CommonConstant.KEY_RESPDESC, e.getMessage());
            } catch (Exception e1) {
                rglog.error(StringUtil.ExceptionToString(e1));
            }
        }
        return CommonConstant.PROCESS_SUCCESS;
    }

    @Override
    protected int bizTerminate(String s) {
        return CommonConstant.PROCESS_SUCCESS;
    }

    /**
     * 进行中营销活动添加商户
     *
     * @param addMchntForToClientMcRequest  进行中活动添加商户接口请求对象
     * @param addMchntForToClientMcResponse 进行中活动添加商户接口应答对象
     * @throws Exception 异常信息
     * @author liujinan
     * @date 2020/6/11
     */
    private BizResponse<AddMchntForToClientMcResponse> underwayMcAddMchnt(AddMchntForToClientMcRequest addMchntForToClientMcRequest, AddMchntForToClientMcResponse addMchntForToClientMcResponse) throws Exception {

        BizResponse<AddMchntForToClientMcResponse> bizResponse = new BizResponse<>();
        rglog.debug("TXN_NUM = {}", addMchntForToClientMcRequest.getHeader().getTxnNum());

        /* 进行中活动批量添加商户 */
        addMchntToUnderwayMc(addMchntForToClientMcRequest);

        bizResponse.setRspSysId(ChannelEnum.MOMP.getChannelType());
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());

        addMchntForToClientMcResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        addMchntForToClientMcResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        bizResponse.setResult(addMchntForToClientMcResponse);
        return bizResponse;
    }

    /**
     * 进行中活动批量添加商户
     *
     * @param addMchntForToClientMcRequest 进行中活动添加商户接口请求对象
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/4/8 21:19
     */
    private void addMchntToUnderwayMc(AddMchntForToClientMcRequest addMchntForToClientMcRequest) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        instId = addMchntForToClientMcRequest.getHeader().getInstId();
        queryId = addMchntForToClientMcRequest.getQueryId();
        mcNo = addMchntForToClientMcRequest.getMcNo();
        mcType = addMchntForToClientMcRequest.getMcType();
        currentTime = DateUtil.getCurrentDateTime(CommonConstant.TIMESTAMP_FORMAT);
        transNo = addMchntForToClientMcRequest.getHeader().getTraceNo();
        rglog.info("判断上送的商户是否是有效数据");
        List<McMchntInfoBean> mchntList = addMchntForToClientMcRequest.getMchntInfoList().stream().filter(item -> !StringUtil.isNullorEmpty(item.getOprType())).collect(Collectors.toList());
        if (mchntList.isEmpty()) {
            /* 无数据需要处理 */
            rglog.error("{}", RespCodeEnum.NO_AVAILABLE_DATA_TO_PROCESS_ERROR.getRespDesc());
            throw new BizException(RespCodeEnum.NO_AVAILABLE_DATA_TO_PROCESS_ERROR.getRespCode(), RespCodeEnum.NO_AVAILABLE_DATA_TO_PROCESS_ERROR.getRespDesc());
        }
        //判断上送商户状态是否正常
        checkBaseMchntStatus(mchntList);
        /* 开启数据库事务 */
        String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.MOMP_POOL_NAME);
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        dbsUtil.dbsBeginTransaction();
        int returnCode;
        PlatBizTmpMapper platBizTmpMapper = new PlatBizTmpMapperImpl();
        McDetailInfoMapper mcDetailInfoMapper = new McDetailInfoMapperImpl();
        //活动转移商户List
        List<McMchntInfoBean> relationList = mchntList.stream().filter(item -> !StringUtil.isNullorEmpty(item.getMcNo()) && !StringUtil.isNullorEmpty(item.getMcName())).collect(Collectors.toList());
        //进行中新增商户list
        List<McMchntInfoBean> underwayList = mchntList.stream().filter(item -> StringUtil.isNullorEmpty(item.getMcNo()) && StringUtil.isNullorEmpty(item.getMcName())).collect(Collectors.toList());

        /* 检查营销活动状态 */
        checkMcStatus(instId, mcNo, mcType);
        if (auditStatus.equals(AuditStatusEnum.NEW_AUDIT_REJECT.getAuditStatusCode()) || auditStatus.equals(AuditStatusEnum.MODIFY_AUDIT_REJECT.getAuditStatusCode()) || auditStatus.equals(AuditStatusEnum.PUBLISH_AUDIT_REJECT.getAuditStatusCode())) {
            rglog.info("进行中添加商户审核或发布被拒，进行修改流程。。。。");
            String globalInstId = addMchntForToClientMcRequest.getHeader().getInstId();
            String globalMcNo = addMchntForToClientMcRequest.getMcNo();
            TMPlatBizTmp platBizTmp = platBizTmpMapper.selectTmpMcInfo(globalInstId, globalMcNo, tableEName);
            /* 活动中添加商户场景 */
            //判断上送的商户信息是否已经存在
            McMchntInfoMapper mcMchntInfoMapper = new McMchntInfoMapperImpl();
            //上送的商户信息
            List<McMchntInfoBean> mcMchntInfoBeanList = underwayList;
            if (null != mcMchntInfoBeanList && mcMchntInfoBeanList.size() > 0) {
                //新增的商户信息
                List<McMchntInfoBean> newmcMchntInfoBeanList = mcMchntInfoBeanList.stream().filter(item -> item.getOprType().equals(CommonConstant.BIZ_OPR_INSERT)).collect(Collectors.toList());
                //删除的商户信息
                List<McMchntInfoBean> deletemcMchntInfoBeanList = mcMchntInfoBeanList.stream().filter(item -> item.getOprType().equals(CommonConstant.BIZ_OPR_DELET)).collect(Collectors.toList());
                //修改的商户信息
                List<McMchntInfoBean> editmcMchntInfoBeanList = mcMchntInfoBeanList.stream().filter(item -> item.getOprType().equals(CommonConstant.BIZ_OPR_UPDATE)).collect(Collectors.toList());
                if (null != deletemcMchntInfoBeanList && deletemcMchntInfoBeanList.size() > 0) {
                    //要删除的商户List
                    List<TMMcMchntInfo> deleteMchntList = deletemcMchntInfoBeanList.stream().map(this::convertMchntInfos).collect(Collectors.toList());
                    //正式表删除商户信息
                    for (TMMcMchntInfo tmMcMchntInfo : deleteMchntList) {
                        returnCode = mcMchntInfoMapper.deleteMchntInfo(tmMcMchntInfo.getInstId(), tmMcMchntInfo.getMcNo(), tmMcMchntInfo.getMchntNo());
                        if (Database.DBS_SUCCESS != returnCode) {
                            dbsUtil.dbsEndTransaction(false);
                            rglog.error("正式表删除商户失败, RETURN_CODE=<{}>, MC_NO=<{}>", returnCode, tmMcMchntInfo.getMcNo());
                            throw new BizException(RespCodeEnum.DELETE_MC_MCHNT_DATA_ERROR.getRespCode(), RespCodeEnum.DELETE_MC_MCHNT_DATA_ERROR.getRespDesc());
                        }
                    }
                    //临时表删除商户信息
                    List<TMPlatBizTmp> deletetmPlatBizTmpList = deleteMchntList.stream().map(this::convertMcMchntInfoToTmps).collect(Collectors.toList());
                    returnCode = platBizTmpMapper.deleteByMcNoMchntNoTablename(deletetmPlatBizTmpList);
                    if (Database.DBS_SUCCESS != returnCode) {
                        dbsUtil.dbsEndTransaction(false);
                        rglog.error("临时删除商户失败, RETURN_CODE=<{}>, MC_NO=<{}>", returnCode, globalMcNo);
                        throw new BizException(RespCodeEnum.DELETE_MC_MCHNT_DATA_ERROR.getRespCode(), RespCodeEnum.DELETE_MC_MCHNT_DATA_ERROR.getRespDesc());
                    }
                }
                if (null != newmcMchntInfoBeanList && newmcMchntInfoBeanList.size() > 0) {
                    //要新增的商户list
                    List<TMMcMchntInfo> addMchntList = newmcMchntInfoBeanList.stream().map(this::convertMchntInfos).collect(Collectors.toList());

                    rglog.error("进行中活动添加商户，校验新增商户是否存在。校验前size=<{}>", addMchntList.size());
                    List<TMMcMchntInfo> newMchntList = new ArrayList<>();
                    for (TMMcMchntInfo mcMchntInfo : addMchntList) {
                        TMMcMchntInfo mchntInfo = mcMchntInfoMapper.selectTMMcMchntInfoByPrimaryKey(globalInstId, globalMcNo, mcMchntInfo.getMchntNo());
                        if (null == mchntInfo) {
                            newMchntList.add(mcMchntInfo);
                        } else {
                            rglog.error("营销活动<{}>商户<{}>已存在，继续下一个", globalMcNo, mcMchntInfo.getMchntNo());
                        }
                    }
                    rglog.error("进行中活动添加商户，校验新增商户是否存在。校验后size=<{}>", newMchntList.size());
                    //手续费营销活动判断商户是否参加其他手续费营销活动
                    if (mcType.equals(McTypeEnum.MCHNT_FEE_DISCOUNT.getMcTypeCode())) {
                        checkMchntStatus(newMchntList);
                    }

                    //插入正式表新增商户
                    returnCode = mcMchntInfoMapper.insertMcMchntInfoListByBank(newMchntList);
                    if (Database.DBS_SUCCESS != returnCode) {
                        dbsUtil.dbsEndTransaction(false);
                        rglog.error("正式表插入新增商户失败, RETURN_CODE=<{}>, MC_NO=<{}>", returnCode, globalMcNo);
                        throw new BizException(RespCodeEnum.WRONG_UPDATEMCMCHNT_ERROR.getRespCode(), RespCodeEnum.WRONG_UPDATEMCMCHNT_ERROR.getRespDesc());
                    }
                    //插入临时表新增商户
                    List<TMPlatBizTmp> tmPlatBizTmpList = newMchntList.stream().map(this::convertMcMchntInfoToTmps).collect(Collectors.toList());
                    returnCode = platBizTmpMapper.insertMchntDataForUnderwayMc(tmPlatBizTmpList);
                    if (Database.DBS_SUCCESS != returnCode) {
                        dbsUtil.dbsEndTransaction(false);
                        rglog.error("临时表插入新增商户失败, RETURN_CODE=<{}>, MC_NO=<{}>", returnCode, globalMcNo);
                        throw new BizException(RespCodeEnum.WRONG_UPDATEMCMCHNT_ERROR.getRespCode(), RespCodeEnum.WRONG_UPDATEMCMCHNT_ERROR.getRespDesc());
                    }
                }
                //查询该活动下进行中已被拒绝的商户信息，如果有则修改为和新增的商户一致状态
                List<TMPlatBizTmp> tmPlatBizTmpList = getAllMcMchntListInfoByInstIdMcNo();
                if (null != tmPlatBizTmpList && tmPlatBizTmpList.size() > 0) {

                    //判断已被拒绝的商户是否参与其他的营销活动
                    List<TMMcMchntInfo> tmMcMchntInfoList = tmPlatBizTmpList.stream().map(this::convertPlatBizToMchntInfo).collect(Collectors.toList());
                    //查询营销商户表进行中被拒绝数据
                    List<TMMcMchntInfo> oldtmMcMchntInfoList = getAllMcMchntListInfoByinstIdMcMoFlag(CommonConstant.MC_EXIT_FLAG, CommonConstant.MCHNT_REFUSE_FLAG);
                    //手续费营销活动判断商户是否参加其他手续费营销活动
                    if (mcType.equals(McTypeEnum.MCHNT_FEE_DISCOUNT.getMcTypeCode())) {
                        checkMchntStatus(oldtmMcMchntInfoList);
                    }
                    for (TMPlatBizTmp tmPlatBizTmp : tmPlatBizTmpList) {
                        tmPlatBizTmp.setBizStatus(BizStatusEnum.TMP_STATUS.getBizStatusCode());
                        tmPlatBizTmp.setMcStatus(McStatusEnum.DRAFT.getMcStatusCode());
                        tmPlatBizTmp.setAuditStatus(AuditStatusEnum.MODIFY_AUDIT_PROCESSING.getAuditStatusCode());
                        rglog.info("更新临时表营销活动<{}>商户<{}>信息状态", tmPlatBizTmp.getMcNo(), tmPlatBizTmp.getMchntNo());
                        returnCode = platBizTmpMapper.updateByInstIdMchntNoMcNo(tmPlatBizTmp);
                        if (Database.DBS_SUCCESS != returnCode) {
                            dbsUtil.dbsEndTransaction(false);
                            rglog.error("更新商户信息失败, RETURN_CODE=<{}>, MC_NO=<{}>", returnCode, globalMcNo);
                            throw new BizException(RespCodeEnum.WRONG_UPDATEMCMCHNT_ERROR.getRespCode(), RespCodeEnum.WRONG_UPDATEMCMCHNT_ERROR.getRespDesc());
                        }
                    }
                    if (oldtmMcMchntInfoList != null && oldtmMcMchntInfoList.size() > 0) {
                        for (TMMcMchntInfo tmMcMchntInfo : oldtmMcMchntInfoList) {
                            tmMcMchntInfo.setNewMchntFlag(CommonConstant.MCHNT_NEW_FLAG);
                        }
                        returnCode = mcMchntInfoMapper.updateMcMchntInfoListByBank(oldtmMcMchntInfoList);
                        if (Database.DBS_SUCCESS != returnCode) {
                            /* 营销活动审批状态异常，不可审批通过 */
                            dbsUtil.dbsEndTransaction(false);
                            rglog.error("{}, RETURN_CODE=<{}>, MC_NO=<{},营销活动商户正式表数据更新失败>", RespCodeEnum.WRONG_UPDATEMCMCHNT_ERROR.getRespDesc(), returnCode, mcNo);
                            throw new BizException(RespCodeEnum.WRONG_UPDATEMCMCHNT_ERROR.getRespCode(), RespCodeEnum.WRONG_UPDATEMCMCHNT_ERROR.getRespDesc());
                        }
                    }
                }
            }
            /* 更新临时表审批状态 */
            /* 修改临时表营销活动状态 */
            TMPlatBizTmp tmPlatBizTmp = new TMPlatBizTmp();
            tmPlatBizTmp.setMcStatus(McStatusEnum.DRAFT.getMcStatusCode());
            tmPlatBizTmp.setBizStatus(BizStatusEnum.TMP_STATUS.getBizStatusCode());
            tmPlatBizTmp.setAuditStatus(AuditStatusEnum.MODIFY_AUDIT_PROCESSING.getAuditStatusCode());
            tmPlatBizTmp.setOprTableCname(tableCName);
            tmPlatBizTmp.setOprTableEname(tableEName);
            returnCode = platBizTmpMapper.updataTMPlatBizByinstIdMcNoTableNameAuditStatus(globalInstId, globalMcNo, tmPlatBizTmp, platBizTmp.getAuditStatus(), queryId, new SimpleDateFormat(CommonConstant.TIMESTAMP_FORMAT).format(new Date()));
            if (Database.DBS_SUCCESS != returnCode) {
                /* 更新临时表中营销活动审批状态失败 */
                dbsUtil.dbsEndTransaction(false);
                rglog.error("更新临时表中营销活动审批状态失败, RETURN_CODE=<{}>, MC_NO=<{}>", returnCode, globalMcNo);
                throw new BizException(RespCodeEnum.UPDATE_TMP_MC_STATUS_FAILED_ERROR.getRespCode(), RespCodeEnum.UPDATE_TMP_MC_STATUS_FAILED_ERROR.getRespDesc());
            }
            if (tableEName.equals(TableNameEnum.T_M_MC_MO_INFO.getTableNameCode())) {
                /* 更新正式表审批状态 */
                TMMcMoneyOffInfo tmMcMoneyOffInfo = new TMMcMoneyOffInfo();
                tmMcMoneyOffInfo.setInstId(instId);
                tmMcMoneyOffInfo.setMcNo(mcNo);
                tmMcMoneyOffInfo.setMcStatus(McStatusEnum.PROGRESSING.getMcStatusCode());
                tmMcMoneyOffInfo.setAuditStatus(AuditStatusEnum.MODIFY_AUDIT_PROCESSING.getAuditStatusCode());
                tmMcMoneyOffInfo.setLastOprId(lastOprId);
                tmMcMoneyOffInfo.setUpdateTime(currentTime);

                returnCode = mcDetailInfoMapper.updateMcStatusForPublishing(tmMcMoneyOffInfo, TableNameEnum.T_M_MC_MO_INFO.getTableNameCode());
                if (Database.DBS_SUCCESS != returnCode) {
                    /* 更新正式表中营销活动审批状态失败 */
                    rglog.error("更新正式表中营销活动审批状态失败, RETURN_CODE=<{}>, MC_NO=<{}>", returnCode, mcNo);
                    throw new BizException(RespCodeEnum.UPDATE_MC_STATUS_FAILED_ERROR.getRespCode(), RespCodeEnum.UPDATE_MC_STATUS_FAILED_ERROR.getRespDesc());
                }
            } else if (tableEName.equals(TableNameEnum.T_M_MC_CP_INFO.getTableNameCode())) {
                /* 更新正式表审批状态 */
                TMMcCouponInfo tmMcCouponInfo = new TMMcCouponInfo();
                tmMcCouponInfo.setInstId(instId);
                tmMcCouponInfo.setMcNo(mcNo);
                tmMcCouponInfo.setMcStatus(McStatusEnum.PROGRESSING.getMcStatusCode());
                tmMcCouponInfo.setAuditStatus(AuditStatusEnum.MODIFY_AUDIT_PROCESSING.getAuditStatusCode());
                tmMcCouponInfo.setLastOprId(lastOprId);
                tmMcCouponInfo.setUpdateTime(currentTime);

                returnCode = mcDetailInfoMapper.updateMcStatusForPublishing(tmMcCouponInfo, TableNameEnum.T_M_MC_CP_INFO.getTableNameCode());
                if (Database.DBS_SUCCESS != returnCode) {
                    /* 更新正式表中营销活动审批状态失败 */
                    rglog.error("更新正式表中营销活动审批状态失败, RETURN_CODE=<{}>, MC_NO=<{}>", returnCode, mcNo);
                    throw new BizException(RespCodeEnum.UPDATE_MC_STATUS_FAILED_ERROR.getRespCode(), RespCodeEnum.UPDATE_MC_STATUS_FAILED_ERROR.getRespDesc());
                }
            } else if (tableEName.equals(TableNameEnum.T_M_MC_MFD_INFO.getTableNameCode())) {
                /* 更新正式表审批状态 */
                TMMcMchntFeeDiscountInfo tmMcMchntFeeDiscountInfo = new TMMcMchntFeeDiscountInfo();
                tmMcMchntFeeDiscountInfo.setInstId(instId);
                tmMcMchntFeeDiscountInfo.setMcNo(mcNo);
                tmMcMchntFeeDiscountInfo.setMcStatus(McStatusEnum.PROGRESSING.getMcStatusCode());
                tmMcMchntFeeDiscountInfo.setAuditStatus(AuditStatusEnum.MODIFY_AUDIT_PROCESSING.getAuditStatusCode());
                tmMcMchntFeeDiscountInfo.setLastOprId(lastOprId);
                tmMcMchntFeeDiscountInfo.setUpdateTime(currentTime);

                returnCode = mcDetailInfoMapper.updateMcStatusForPublishing(tmMcMchntFeeDiscountInfo, TableNameEnum.T_M_MC_MFD_INFO.getTableNameCode());
                if (Database.DBS_SUCCESS != returnCode) {
                    /* 更新正式表中营销活动审批状态失败 */
                    rglog.error("更新正式表中营销活动审批状态失败, RETURN_CODE=<{}>, MC_NO=<{}>", returnCode, mcNo);
                    throw new BizException(RespCodeEnum.UPDATE_MC_STATUS_FAILED_ERROR.getRespCode(), RespCodeEnum.UPDATE_MC_STATUS_FAILED_ERROR.getRespDesc());
                }
            }
        } else {
            /* 内外报文转换 */
            List<TMPlatBizTmp> tmPlatBizTmpList = new ArrayList<>();
            List<TMMcMchntInfo> tmMcMchntInfoList = new ArrayList<>();
            rglog.info("进行中添加商户且活动审批状态为发布通过，上送商户列表若无oprType则不处理");
            underwayList.forEach(item -> convertMcMchntInfo(item, instId, mcNo, queryId, currentTime, tmPlatBizTmpList));
            underwayList.forEach(item -> convertMcMchntInfos(item, instId, mcNo, queryId, currentTime, tmMcMchntInfoList));
            if (mcType.equals(McTypeEnum.MCHNT_FEE_DISCOUNT.getMcTypeCode())) {
                //判断上送商户是否参加其他手续费营销活动
                checkMchntStatus(tmMcMchntInfoList);
            }
            if (tmPlatBizTmpList.isEmpty() && relationList.isEmpty()) {
                /* 无数据需要处理 */
                rglog.error("{}", RespCodeEnum.NO_AVAILABLE_DATA_TO_PROCESS_ERROR.getRespDesc());
                throw new BizException(RespCodeEnum.NO_AVAILABLE_DATA_TO_PROCESS_ERROR.getRespCode(), RespCodeEnum.NO_AVAILABLE_DATA_TO_PROCESS_ERROR.getRespDesc());
            }
            rglog.info("共计<{}>条商户数据等待处理", tmPlatBizTmpList.size() + relationList.size());
            List<TMPlatBizTmp> tmPlatBizTmpList1 = new ArrayList<>();
            //进行中新增商户不为空
            if (tmPlatBizTmpList.size() > 0) {
                rglog.info("处理正常进行中活动添加商户流程。。。。。。");
                /* 循环查询商户是否已经增加 如果已新增则不新增 */
                for (TMPlatBizTmp tmPlatBizTmp : tmPlatBizTmpList) {
                    List<TMPlatBizTmp> list = platBizTmpMapper.selectListByInstIdMcNoTableNameStatus(tmPlatBizTmp);
                    if (null == list || CommonConstant.ZERO == list.size()) {
                        tmPlatBizTmpList1.add(tmPlatBizTmp);
                    } else if (CommonConstant.ZERO < list.size()) {
                        rglog.error("法人机构<{}>的进行中的营销活动<{}>待添加商户<{}>已被添加，不可重复添加!", instId, mcNo, tmPlatBizTmp.getMchntNo());
                        // 功能调整为支持已存在的商户重复上送，营销去重，不抛异常
//                        throw new BizException(RespCodeEnum.MCHNT_HAS_BEEN_ADDED.getRespCode(), RespCodeEnum.MCHNT_HAS_BEEN_ADDED.getRespDesc());
                    } else {

                    }
                }
                rglog.error("法人机构<{}>的进行中的营销活动<{}>待添加商户size=<{}>!", instId, mcNo, tmPlatBizTmpList1.size());
                //增加判断，临时表集合为空，则认为所有商户重复，不更新数据库
                if (!tmPlatBizTmpList1.isEmpty()) {
                    //添加正式表、临时表数据校验，
                    McMchntInfoMapper mcMchntInfoMapper = new McMchntInfoMapperImpl();
                    returnCode = mcMchntInfoMapper.insertMcMchntInfoListByBank(tmMcMchntInfoList);
                    if (Database.DBS_SUCCESS != returnCode) {
                        /* 异常结束数据库事务 */
                        rglog.error("新增营销活动商户信息失败, RETURN_CODE=<{}>, MC_NO=<{}>", returnCode, instId, mcNo, mcType);
                        throw new BizException(RespCodeEnum.DATABASE_OPR_ERROR.getRespCode(), RespCodeEnum.DATABASE_OPR_ERROR.getRespDesc());
                    }
                    /* 插入新增商户数据数据 */
                    int returnCodes = platBizTmpMapper.insertMchntDataForUnderwayMc(tmPlatBizTmpList1);
                    if (Database.DBS_SUCCESS != returnCodes) {
                        /* 异常结束数据库事务 */
                        rglog.error("新增营销活动商户信息失败, RETURN_CODE=<{}>, MC_NO=<{}>", returnCode, instId, mcNo, mcType);
                        throw new BizException(RespCodeEnum.DATABASE_OPR_ERROR.getRespCode(), RespCodeEnum.DATABASE_OPR_ERROR.getRespDesc());
                    }
                }
            }
            if (relationList.size() > 0) {
                rglog.info("处理活动转移商户流程。。。。。");
                //转化商户转移List
                List<TMMcMchntInfo> relationMchntList = relationList.stream().map(this::convertMchntInfos).collect(Collectors.toList());
                for (McMchntInfoBean mcMchntInfoBean : relationList) {
                    //往新营销活动插入商户正式表信息
                    insertTMMcMchntInfo(mcMchntInfoBean, dbsUtil);
                    //更新旧营销活动商户正式表信息
                    updateTMMcMchntInfoByMcNo(mcMchntInfoBean, dbsUtil);
                }
                //将商户正式表信息转换为临时表信息
                List<TMPlatBizTmp> tmPlatBizTmpLists = new ArrayList<>();
                relationList.forEach(item -> convertMcMchntInfo(item, instId, mcNo, queryId, currentTime, tmPlatBizTmpLists));
                //将临时表信息插入数据库
                returnCode = platBizTmpMapper.insertMchntDataForUnderwayMc(tmPlatBizTmpLists);
                if (Database.DBS_SUCCESS != returnCode) {
                    dbsUtil.dbsEndTransaction(false);
                    rglog.error("临时表插入新增商户失败, RETURN_CODE=<{}>, MC_NO=<{}>", returnCode, mcNo);
                    throw new BizException(RespCodeEnum.WRONG_UPDATEMCMCHNT_ERROR.getRespCode(), RespCodeEnum.WRONG_UPDATEMCMCHNT_ERROR.getRespDesc());
                }
            }
            if (CommonConstant.ZERO < tmPlatBizTmpList1.size() || relationList.size() > 0) {

                /* 修改临时表营销活动状态 */
                TMPlatBizTmp tmPlatBizTmp = new TMPlatBizTmp();
                tmPlatBizTmp.setMcStatus(McStatusEnum.DRAFT.getMcStatusCode());
                tmPlatBizTmp.setBizStatus(BizStatusEnum.TMP_STATUS.getBizStatusCode());
                tmPlatBizTmp.setAuditStatus(AuditStatusEnum.NEW_AUDIT_PROCESSING.getAuditStatusCode());
                tmPlatBizTmp.setOprTableCname(tableCName);
                tmPlatBizTmp.setOprTableEname(tableEName);
                returnCode = platBizTmpMapper.updataTMPlatBizByinstIdMcNoTableNameAuditStatus(instId, mcNo, tmPlatBizTmp, AuditStatusEnum.PUBLISH_AUDIT_PASSED.getAuditStatusCode(), lastOprId, new SimpleDateFormat(CommonConstant.TIMESTAMP_FORMAT).format(new Date()));
                if (Database.DBS_SUCCESS != returnCode) {
                    rglog.error("更新法人机构<{}>的营销活动<{}>的状态,营销活动状态和审批状态失败！ RETURN_CODE=<{}>", instId, mcNo, returnCode);
                    throw new BizException(RespCodeEnum.UPDATE_TMP_MC_STATUS_FAILED_ERROR.getRespCode(), RespCodeEnum.UPDATE_TMP_MC_STATUS_FAILED_ERROR.getRespDesc());
                }
                if (tableEName.equals(TableNameEnum.T_M_MC_MO_INFO.getTableNameCode())) {
                    /* 更新正式表审批状态 */
                    TMMcMoneyOffInfo tmMcMoneyOffInfo = new TMMcMoneyOffInfo();
                    tmMcMoneyOffInfo.setInstId(instId);
                    tmMcMoneyOffInfo.setMcNo(mcNo);
                    tmMcMoneyOffInfo.setMcStatus(McStatusEnum.PROGRESSING.getMcStatusCode());
                    tmMcMoneyOffInfo.setAuditStatus(AuditStatusEnum.NEW_AUDIT_PROCESSING.getAuditStatusCode());
                    tmMcMoneyOffInfo.setLastOprId(lastOprId);
                    tmMcMoneyOffInfo.setUpdateTime(currentTime);

                    returnCode = mcDetailInfoMapper.updateMcStatusForPublishing(tmMcMoneyOffInfo, TableNameEnum.T_M_MC_MO_INFO.getTableNameCode());
                    if (Database.DBS_SUCCESS != returnCode) {
                        /* 更新正式表中营销活动审批状态失败 */
                        rglog.error("更新正式表中营销活动审批状态失败, RETURN_CODE=<{}>, MC_NO=<{}>", returnCode, mcNo);
                        throw new BizException(RespCodeEnum.UPDATE_MC_STATUS_FAILED_ERROR.getRespCode(), RespCodeEnum.UPDATE_MC_STATUS_FAILED_ERROR.getRespDesc());
                    }
                } else if (tableEName.equals(TableNameEnum.T_M_MC_CP_INFO.getTableNameCode())) {
                    /* 更新正式表审批状态 */
                    TMMcCouponInfo tmMcCouponInfo = new TMMcCouponInfo();
                    tmMcCouponInfo.setInstId(instId);
                    tmMcCouponInfo.setMcNo(mcNo);
                    tmMcCouponInfo.setMcStatus(McStatusEnum.PROGRESSING.getMcStatusCode());
                    tmMcCouponInfo.setAuditStatus(AuditStatusEnum.NEW_AUDIT_PROCESSING.getAuditStatusCode());
                    tmMcCouponInfo.setLastOprId(lastOprId);
                    tmMcCouponInfo.setUpdateTime(currentTime);

                    returnCode = mcDetailInfoMapper.updateMcStatusForPublishing(tmMcCouponInfo, TableNameEnum.T_M_MC_CP_INFO.getTableNameCode());
                    if (Database.DBS_SUCCESS != returnCode) {
                        /* 更新正式表中营销活动审批状态失败 */
                        rglog.error("更新正式表中营销活动审批状态失败, RETURN_CODE=<{}>, MC_NO=<{}>", returnCode, mcNo);
                        throw new BizException(RespCodeEnum.UPDATE_MC_STATUS_FAILED_ERROR.getRespCode(), RespCodeEnum.UPDATE_MC_STATUS_FAILED_ERROR.getRespDesc());
                    }
                } else if (tableEName.equals(TableNameEnum.T_M_MC_MFD_INFO.getTableNameCode())) {
                    /* 更新正式表审批状态 */
                    TMMcMchntFeeDiscountInfo tmMcMchntFeeDiscountInfo = new TMMcMchntFeeDiscountInfo();
                    tmMcMchntFeeDiscountInfo.setInstId(instId);
                    tmMcMchntFeeDiscountInfo.setMcNo(mcNo);
                    tmMcMchntFeeDiscountInfo.setMcStatus(McStatusEnum.PROGRESSING.getMcStatusCode());
                    tmMcMchntFeeDiscountInfo.setAuditStatus(AuditStatusEnum.NEW_AUDIT_PROCESSING.getAuditStatusCode());
                    tmMcMchntFeeDiscountInfo.setLastOprId(lastOprId);
                    tmMcMchntFeeDiscountInfo.setUpdateTime(currentTime);

                    returnCode = mcDetailInfoMapper.updateMcStatusForPublishing(tmMcMchntFeeDiscountInfo, TableNameEnum.T_M_MC_MFD_INFO.getTableNameCode());
                    if (Database.DBS_SUCCESS != returnCode) {
                        /* 更新正式表中营销活动审批状态失败 */
                        rglog.error("更新正式表中营销活动审批状态失败, RETURN_CODE=<{}>, MC_NO=<{}>", returnCode, mcNo);
                        throw new BizException(RespCodeEnum.UPDATE_MC_STATUS_FAILED_ERROR.getRespCode(), RespCodeEnum.UPDATE_MC_STATUS_FAILED_ERROR.getRespDesc());
                    }
                }

            }
        }
        /* 正常结束数据库事务 */
        dbsUtil.dbsEndTransaction(true);
    }

    /**
     * 检查营销活动状态是否是进行中
     *
     * @param instId 法人机构号
     * @param mcNo   营销活动编号
     * @param mcType 营销活动类型
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/4/8 22:03
     */
    private void checkMcStatus(String instId, String mcNo, String mcType) throws Exception {
        String mcStatus = null;
        McDetailInfoMapper mcDetailInfoMapper = new McDetailInfoMapperImpl();

        /* 查询营销活动信息判断是否可以添加商户 */
        if (McTypeEnum.REDUCTION_FIXED_AMOUNT.getMcTypeCode().equals(mcType) || McTypeEnum.REDUCTION_RANDOM_AMOUNT.getMcTypeCode().equals(mcType) ||
                McTypeEnum.REDUCTION_FIXED_RATE.getMcTypeCode().equals(mcType) || McTypeEnum.REDUCTION_RANDOM_RATE.getMcTypeCode().equals(mcType)) {

            /* 减免类营销活动 */
            TMMcMoneyOffInfo tmMcMoneyOffInfo = mcDetailInfoMapper.selectMoneyOffMcDetailInfo(instId, mcNo);
            if (null == tmMcMoneyOffInfo) {
                /* 未取到营销活动基本信息数据,不可添加商户 */
                rglog.error("{}, INST_ID=<{}>, MC_NO=<{}>", RespCodeEnum.GET_MONEY_OFF_MC_DETAIL_ERROR.getRespDesc(), instId, mcNo);
                throw new BizException(RespCodeEnum.GET_MONEY_OFF_MC_DETAIL_ERROR.getRespCode(), RespCodeEnum.GET_MONEY_OFF_MC_DETAIL_ERROR.getRespDesc());
            }

            tableEName = TableNameEnum.T_M_MC_MO_INFO.getTableNameCode();
            tableCName = TableNameEnum.T_M_MC_MO_INFO.getTableNameDesc();

            /* 检查营销活动出资方，只有银行发起的营销活动可以在进行中添加商户 */
            checkToClientMcSponsor(instId, mcNo, tmMcMoneyOffInfo.getSponsor());

            mcStatus = tmMcMoneyOffInfo.getMcStatus();
            auditStatus = tmMcMoneyOffInfo.getAuditStatus();

        } else if (McTypeEnum.COUPON_FREE.getMcTypeCode().equals(mcType) || McTypeEnum.COUPON_PAID.getMcTypeCode().equals(mcType) || McTypeEnum.COUPON_ALL.getMcTypeCode().equals(mcType)) {

            /* 卡券类营销活动 */
            TMMcCouponInfo tmMcCouponInfo = mcDetailInfoMapper.selectCouponMcDetailInfo(instId, mcNo);
            if (null == tmMcCouponInfo) {
                /* 未取到营销活动基本信息数据,不可添加商户 */
                rglog.error("{}, INST_ID=<{}>, MC_NO=<{}>", RespCodeEnum.GET_COUPON_MC_DETAIL_ERROR.getRespDesc(), instId, mcNo);
                throw new BizException(RespCodeEnum.GET_COUPON_MC_DETAIL_ERROR.getRespCode(), RespCodeEnum.GET_COUPON_MC_DETAIL_ERROR.getRespDesc());
            }

            tableEName = TableNameEnum.T_M_MC_CP_INFO.getTableNameCode();
            tableCName = TableNameEnum.T_M_MC_CP_INFO.getTableNameDesc();

            /* 检查营销活动出资方，只有银行发起的营销活动可以在进行中添加商户 */
            checkToClientMcSponsor(instId, mcNo, tmMcCouponInfo.getSponsor());

            mcStatus = tmMcCouponInfo.getMcStatus();
            auditStatus = tmMcCouponInfo.getAuditStatus();
        } else if (McTypeEnum.MCHNT_FEE_DISCOUNT.getMcTypeCode().equals(mcType)) {

            /* 商户手续费优惠类营销活动 */
            TMMcMchntFeeDiscountInfo tmMcMchntFeeDiscountInfo = mcDetailInfoMapper.selectMchntFeeDiscountMcDetailInfo(instId, mcNo);
            if (null == tmMcMchntFeeDiscountInfo) {
                /* 未取到营销活动基本信息数据,不可添加商户 */
                rglog.error("{}, INST_ID=<{}>, MC_NO=<{}>", RespCodeEnum.WRONG_ADD_MCHNT.getRespDesc(), instId, mcNo);
                throw new BizException(RespCodeEnum.WRONG_ADD_MCHNT.getRespCode(), RespCodeEnum.WRONG_ADD_MCHNT.getRespDesc());
            }
            if (tmMcMchntFeeDiscountInfo.getNewMchntFlag().equals(CommonConstant.MFD_NEW_MCHNT_CFG) && tmMcMchntFeeDiscountInfo.getStockMchntFlag().equals(CommonConstant.STOCK_MCHNT_FLAG_ZERO)) {
                /* 该活动支持新增商户,不可进行中添加商户 */
                rglog.error("{}, INST_ID=<{}>, MC_NO=<{}>", RespCodeEnum.WRONG_ADD_MCHNT.getRespDesc(), instId, mcNo);
                throw new BizException(RespCodeEnum.WRONG_ADD_MCHNT.getRespCode(), RespCodeEnum.WRONG_ADD_MCHNT.getRespDesc());
            } else {
                //判断商户是否为新入驻商户
                MchntInfoMapper mchntInfoMapper = new MchntInfoMapperImpl();
                if (null != mcMchntInfoBeanList && mcMchntInfoBeanList.size() > 0) {
                    for (McMchntInfoBean mcMchntInfoBean : mcMchntInfoBeanList) {
                        TBMchntBaseInfo tbMchntBaseInfo = mchntInfoMapper.selectMompMchntBaseInfoByPrimaryKey(instId, mcMchntInfoBean.getMchntNo());
                        //判断商户的的入驻日期是否在活动时间内
                        int code = MompDateUtil.checkDateRange(tbMchntBaseInfo.getMchntExpandTime().substring(0, 7).replaceAll("-", ""), tmMcMchntFeeDiscountInfo.getMcStartDate(), tmMcMchntFeeDiscountInfo.getMcEndDate());
                        //0-商户入驻时间在活动开始，结束时间之间
                        if (code == 0) {
                            /* 该活动不可进行中添加商户 */
                            rglog.error("{}, INST_ID=<{}>, MC_NO=<{}>", RespCodeEnum.WRONG_ADD_MCHNT.getRespDesc(), instId, mcNo);
                            throw new BizException(RespCodeEnum.WRONG_ADD_MCHNT.getRespCode(), RespCodeEnum.WRONG_ADD_MCHNT.getRespDesc());
                        }
                    }
                }
            }

            tableEName = TableNameEnum.T_M_MC_MFD_INFO.getTableNameCode();
            tableCName = TableNameEnum.T_M_MC_MFD_INFO.getTableNameDesc();

            mcStatus = tmMcMchntFeeDiscountInfo.getMcStatus();
            auditStatus = tmMcMchntFeeDiscountInfo.getAuditStatus();

        } else {
            /* 营销活动类型异常,不可添加商户 */
            rglog.error("{}, INST_ID=<{}>, MC_NO=<{}>, MC_TYPE=<{}>", RespCodeEnum.WRONG_MC_TYPE_TO_ADD_MCHNT_ERROR.getRespDesc(), instId, mcNo, mcType);
            throw new BizException(RespCodeEnum.WRONG_MC_TYPE_TO_ADD_MCHNT_ERROR.getRespCode(), RespCodeEnum.WRONG_MC_TYPE_TO_ADD_MCHNT_ERROR.getRespDesc());
        }

        /* 检查营销活动状态是否是进行中 */
        if (null == mcStatus || !McStatusEnum.PROGRESSING.getMcStatusCode().equals(mcStatus)) {
            /* 营销活动状态异常,不可添加商户 */
            rglog.error("{}, INST_ID=<{}>, MC_NO=<{}>, MC_STATUS=<{}>", RespCodeEnum.WRONG_MC_STATUS_TO_ADD_MCHNT_ERROR.getRespDesc(), instId, mcNo, mcStatus);
            throw new BizException(RespCodeEnum.WRONG_MC_STATUS_TO_ADD_MCHNT_ERROR.getRespCode(), RespCodeEnum.WRONG_MC_STATUS_TO_ADD_MCHNT_ERROR.getRespDesc());
        }
    }

    /**
     * 检查对客类营销活动出资方是否是银行
     * 是银行流程继续
     * 不是银行抛异常
     *
     * @param instId  法人机构号
     * @param mcNo    营销活动编号
     * @param sponsor 营销活动出资方
     * @throws BizException 异常信息
     * @author Jinan Liu
     * @date 2020/7/16 10:59
     */
    private void checkToClientMcSponsor(String instId, String mcNo, String sponsor) throws BizException {

        if (!StringUtil.isNullorEmpty(sponsor)) {
            if (!CommonConstant.SPONSOR_BANK.equals(sponsor) &&
                    !CommonConstant.SPONSOR_MCHNT_NORECHARGE.equals(sponsor)) {
                /* 未取到营销活动基本信息数据,不可添加商户 */
                rglog.error("{}, INST_ID=<{}>, MC_NO=<{}>", RespCodeEnum.BANK_SPONSOR_CAN_ADD_MCHNT_ERROR.getRespDesc(), instId, mcNo);
                throw new BizException(RespCodeEnum.BANK_SPONSOR_CAN_ADD_MCHNT_ERROR.getRespCode(), RespCodeEnum.BANK_SPONSOR_CAN_ADD_MCHNT_ERROR.getRespDesc());
            }
        } else {
            /* 数据库数据异常，请检查 */
            rglog.error("{}, INST_ID=<{}>, MC_NO=<{}>, TABLE_NAME=<{}>, ATTRIBUTE=<{}>",
                    RespCodeEnum.DATABASE_DATA_ABNORMAL_ERROR.getRespDesc(), instId, mcNo, tableEName, DatabaseLabelConstant.SPONSOR);
            throw new BizException(RespCodeEnum.DATABASE_DATA_ABNORMAL_ERROR.getRespCode(), RespCodeEnum.DATABASE_DATA_ABNORMAL_ERROR.getRespDesc());
        }
    }

    /**
     * 营销活动信息数据库实体类和接口实体类数据转换
     *
     * @param mcMchntInfoBean 营销活动商户信息对象
     * @param instId          法人机构号
     * @param mcNo            营销活动编号
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/4/9 09:15
     */
    private void convertMcMchntInfo(McMchntInfoBean mcMchntInfoBean, String instId, String mcNo, String oprId, String currentTime, List<TMPlatBizTmp> tmPlatBizTmpList) {

        TMPlatBizTmp tmPlatBizTmp = new TMPlatBizTmp();

        try {

            Map<String, String> bizDataMap = new HashMap<>();
            bizDataMap.put(DatabaseLabelConstant.INST_ID, instId);
            bizDataMap.put(DatabaseLabelConstant.MC_NO, mcNo);
            bizDataMap.put(DatabaseLabelConstant.MCHNT_NO, mcMchntInfoBean.getMchntNo());
            bizDataMap.put(DatabaseLabelConstant.MCHNT_TYPE, mcMchntInfoBean.getMchntType());
            bizDataMap.put(DatabaseLabelConstant.MCHNT_NAME, mcMchntInfoBean.getMchntName());
            bizDataMap.put(DatabaseLabelConstant.NEW_MCHNT_FLAG, CommonConstant.MCHNT_OLD_FLAG);
            bizDataMap.put(DatabaseLabelConstant.EXIT_FLAG, CommonConstant.MC_NOT_EXIT_FLAG);
            bizDataMap.put(DatabaseLabelConstant.OPR_ID, oprId);
            bizDataMap.put(DatabaseLabelConstant.CREATE_TIME, currentTime);

            String bizDataString = JSON.toJSONString(bizDataMap);

            /* 法人机构标识 */
            tmPlatBizTmp.setInstId(instId);
            /* 机构编号 */
            tmPlatBizTmp.setOrgId(CommonConstant.EMPTY_STRING);
            /* 商户号 */
            tmPlatBizTmp.setMchntNo(mcMchntInfoBean.getMchntNo());
            /* 商户名称 */
            tmPlatBizTmp.setMchntName(mcMchntInfoBean.getMchntName());
            /* 营销活动编号 */
            tmPlatBizTmp.setMcNo(mcNo);
            /* 用户标识 */
            tmPlatBizTmp.setUserId(CommonConstant.EMPTY_STRING);
            /* 代理商编号 */
            tmPlatBizTmp.setAgentMchntNo(CommonConstant.EMPTY_STRING);
            /* 记录索引 */
            tmPlatBizTmp.setRecIndex(CommonConstant.ZERO);
            /* 操作菜单 */
            tmPlatBizTmp.setOprMenuId(CommonConstant.DEFAULT_MENU_ID);
            /* 操作菜单名 */
            tmPlatBizTmp.setOprMenuName(CommonConstant.DEFAULT_MENU_NAME);
            /* 操作表中文名 */
            tmPlatBizTmp.setOprTableCname(TableNameEnum.T_M_MC_MCHNT_INFO.getTableNameDesc());
            /* 操作表英文名 */
            tmPlatBizTmp.setOprTableEname(TableNameEnum.T_M_MC_MCHNT_INFO.getTableNameCode());
            /* 操作 */
            tmPlatBizTmp.setBizOpr(CommonConstant.BIZ_OPR_INSERT);
            /* 数据域1 */
            tmPlatBizTmp.setBizData1(bizDataString);
            /* 数据域2 */
            tmPlatBizTmp.setBizData2(CommonConstant.EMPTY_STRING);
            /* 数据域3 */
            tmPlatBizTmp.setBizData3(CommonConstant.EMPTY_STRING);
            /* 审批拒绝原因 */
            tmPlatBizTmp.setAuditRefuseReason(CommonConstant.EMPTY_STRING);
            /* 状态 */
            tmPlatBizTmp.setBizStatus(BizStatusEnum.TMP_STATUS.getBizStatusCode());
            /* 营销活动状态 */
            tmPlatBizTmp.setMcStatus(McStatusEnum.DRAFT.getMcStatusCode());
            /* 审批状态 */
            tmPlatBizTmp.setAuditStatus(AuditStatusEnum.NEW_AUDIT_PROCESSING.getAuditStatusCode());
            /* 创建人 */
            tmPlatBizTmp.setOprId(oprId);
            /* 创建时间戳 */
            tmPlatBizTmp.setCreateTime(currentTime);
            /* 最后修改人 */
            tmPlatBizTmp.setLastOprId(CommonConstant.EMPTY_STRING);
            /* 最后修改时间 */
            tmPlatBizTmp.setLastUpdateTime(CommonConstant.EMPTY_STRING);
            /* 备用字段1 */
            /* 使用remark1字段存储商户范围 */
            tmPlatBizTmp.setRemark1(mcMchntInfoBean.getMchntType());
            /* 备用字段2 */
            tmPlatBizTmp.setRemark2(CommonConstant.EMPTY_STRING);
            /* 备用字段3 */
            tmPlatBizTmp.setRemark3(CommonConstant.EMPTY_STRING);
            /* 备用字段4 */
            tmPlatBizTmp.setRemark4(CommonConstant.EMPTY_STRING);
            /* 备用字段5 */
            tmPlatBizTmp.setRemark5(CommonConstant.EMPTY_STRING);

            tmPlatBizTmpList.add(tmPlatBizTmp);

        } catch (Exception e) {
            rglog.error("转换营销活动商户信息异常,异常信息:<{}>", StringUtil.ExceptionToString(e));
            throw new BizException(RespCodeEnum.SYSTEM_ERROR.getRespCode(), RespCodeEnum.SYSTEM_ERROR.getRespDesc());
        }
    }

    /**
     * 请求报文检查
     *
     * @param addMchntForToClientMcRequest 进行中活动添加商户接口请求对象
     * @throws BizException 异常信息
     * @author Jinan Liu
     * @date 2020/7/16 10:14
     */
    private void messageValidation(AddMchntForToClientMcRequest addMchntForToClientMcRequest) throws BizException {

        /* 检查交易日期 */
        if (StringUtil.isNullorEmpty(addMchntForToClientMcRequest.getHeader().getTransDate())) {
            rglog.error("交易日期必须出现!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }

        /* 检查交易时间 */
        if (StringUtil.isNullorEmpty(addMchntForToClientMcRequest.getHeader().getTransTime())) {
            rglog.error("交易时间必须出现!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }

        /* 检查机构标识 */
        if (StringUtil.isNullorEmpty(addMchntForToClientMcRequest.getHeader().getInstId())) {
            rglog.error("机构标识必须出现!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }

        /* 检查交易流水号 */
        if (StringUtil.isNullorEmpty(addMchntForToClientMcRequest.getHeader().getTraceNo())) {
            rglog.error("交易流水号必须出现!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }

        /* 检查查询类型 */
        if (StringUtil.isNullorEmpty(addMchntForToClientMcRequest.getQueryType())) {
            rglog.error("查询类型必须出现!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }

        /* 检查查询主体 */
        if (StringUtil.isNullorEmpty(addMchntForToClientMcRequest.getQueryId())) {
            rglog.error("查询主体必须出现!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        } else {
            lastOprId = addMchntForToClientMcRequest.getQueryId();
        }

        /* 检查查询主体所属机构 */
        if (StringUtil.isNullorEmpty(addMchntForToClientMcRequest.getQueryOrgId())) {
            rglog.error("查询主体所属机构必须出现!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }

        /* 检查查询主体角色*/
        if (StringUtil.isNullorEmpty(addMchntForToClientMcRequest.getQueryRole())) {
            rglog.error("查询主体角色必须出现!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }

        /* 检查营销活动编号 */
        if (StringUtil.isNullorEmpty(addMchntForToClientMcRequest.getMcNo())) {
            rglog.error("营销活动编号必须出现!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }

        /* 检查营销活动类型 */
        if (StringUtil.isNullorEmpty(addMchntForToClientMcRequest.getMcType())) {
            rglog.error("营销活动类型必须出现!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }

        /* 检查商户信息 */
        List<McMchntInfoBean> mcMchntInfoBeanList = addMchntForToClientMcRequest.getMchntInfoList();
        if (null == mcMchntInfoBeanList || mcMchntInfoBeanList.isEmpty()) {
            rglog.error("商户信息必须出现!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }
    }

    /**
     * 营销活动商户信息数据库实体类和接口实体类数据转换
     *
     * @param mcMchntInfoBean 营销活动商户信息对象
     * @return cc.rengu.igas.momp.common.entity.TMMcMchntInfo 营销活动商户信息表
     * @author JL Pang
     * @since 2020/4/1 21:10
     */
    private void convertMcMchntInfos(McMchntInfoBean mcMchntInfoBean, String instId, String mcNo, String oprId, String currentTime, List<TMMcMchntInfo> tmMcMchntInfoList) {
        TMMcMchntInfo mcMchntInfo = new TMMcMchntInfo();
        try {
            if (McTypeEnum.MCHNT_FEE_DISCOUNT.getMcTypeCode().equals(mcType)) {
                MchntInfoMapper mchntInfoMapper = new MchntInfoMapperImpl();
                //判断商户是否存在特殊费率
                List<TBMchntFeeInfo> tbMchntFeeInfoList = mchntInfoMapper.selectMchntFeeInfoListByMchntNo(instId, mcMchntInfoBean.getMchntNo());
                if (null != tbMchntFeeInfoList && tbMchntFeeInfoList.size() > 0) {
                    tbMchntFeeInfoList = tbMchntFeeInfoList.stream().filter(item -> item.getFeeInputMode().equals(CommonConstant.ZERO_COMMON_CONSTANT)).collect(Collectors.toList());
                    if (tbMchntFeeInfoList.size() > 0) {
                        rglog.error("商户<{}>费率信息表存在特殊费率！", mcMchntInfoBean.getMchntNo());
                        throw new BizException(RespCodeEnum.EXISTENCE_SPECIAL_FEE.getRespCode(), RespCodeEnum.EXISTENCE_SPECIAL_FEE.getRespDesc());
                    }
                }
                //查询手续费达标统计表是否有该商户现交易日期达标的数据
                TMStatMfdCriteriaMapper tmStatMfdCriteriaMapper = new TMStatMfdCriteriaMapperImpl();
                TMStatMfdCriteria tmStatMfdCriteria = tmStatMfdCriteriaMapper.selectTMStatMfdCriteriaByUsingMchntNoAndDateRange(instId, mcMchntInfoBean.getMchntNo(), DateUtil.getCurrentDate());
                if (null != tmStatMfdCriteria) {
                    /* 营销活动信息正式表查询 */
                    McDetailInfoMapper mcDetailInfoMapper = new McDetailInfoMapperImpl();
                    TMMcMchntFeeDiscountInfo tmMcMchntFeeDiscountInfo = (TMMcMchntFeeDiscountInfo) mcDetailInfoMapper.selectMcDetailInfo(instId, tmStatMfdCriteria.getMcNo(), TableNameEnum.T_M_MC_MFD_INFO.getTableNameCode());
                    //如果达标统计表有当前交易日期商户达标的数据，则直接拒绝新增,原活动状态为自然结束，强制结束，已结束的数据作废
                    if (null != tmMcMchntFeeDiscountInfo && !tmMcMchntFeeDiscountInfo.getMcStatus().equals(McStatusEnum.ENDED.getMcStatusCode()) && !tmMcMchntFeeDiscountInfo.getMcStatus().equals(McStatusEnum.FORCE_ENDED.getMcStatusCode()) && !tmMcMchntFeeDiscountInfo.getMcStatus().equals(McStatusEnum.ALL_ENDED.getMcStatusCode())) {
                        rglog.error("商户<{}>已参加其他手续费活动<{}>！", mcMchntInfoBean.getMchntNo(), tmMcMchntFeeDiscountInfo.getMcNo());
                        throw new BizException(RespCodeEnum.WRONG_MCHNT_ISALREADY_INMC_ERROR.getRespCode(), RespCodeEnum.WRONG_MCHNT_ISALREADY_INMC_ERROR.getRespDesc() + ",商户<" + mcMchntInfoBean.getMchntNo() + ">");
                    }
                }
            }
            /* 法人机构号 */
            mcMchntInfo.setInstId(instId);
            /* 营销活动编号 */
            mcMchntInfo.setMcNo(mcNo);
            /* 商户范围类型 */
            mcMchntInfo.setMchntType(mcMchntInfoBean.getMchntType());
            /* 商户范围号 */
            mcMchntInfo.setMchntNo(mcMchntInfoBean.getMchntNo());
            /* 商户范围名称 */
            mcMchntInfo.setMchntName(mcMchntInfoBean.getMchntName());
            /* 新增商户标志 */
            mcMchntInfo.setNewMchntFlag(CommonConstant.MCHNT_NEW_FLAG);
            /* 退出标志 */
            mcMchntInfo.setExitFlag(CommonConstant.MC_EXIT_FLAG);
            /* 创建人 */
            mcMchntInfo.setOprId(oprId);
            /* 创建时间 */
            mcMchntInfo.setCreateTime(currentTime);
            tmMcMchntInfoList.add(mcMchntInfo);
        } catch (Exception e) {
            rglog.error("转换营销活动商户信息异常,异常信息:<{}>", StringUtil.ExceptionToString(e));
            throw new BizException(RespCodeEnum.SYSTEM_ERROR.getRespCode(), e.getMessage());
        }
    }

    /**
     * 营销活动商户信息数据库实体类和接口实体类数据转换(进行中新增商户)
     *
     * @param mcMchntInfoBean 营销活动商户信息对象
     * @return cc.rengu.igas.momp.common.entity.TMMcMchntInfo 营销活动商户信息表
     * @author JL PANG
     * @since 2020/7/20
     */
    private TMMcMchntInfo convertMchntInfos(McMchntInfoBean mcMchntInfoBean) {
        TMMcMchntInfo mcMchntInfo = new TMMcMchntInfo();
        try {
            /* 法人机构号 */
            mcMchntInfo.setInstId(instId);
            /* 营销活动编号 */
            mcMchntInfo.setMcNo(mcNo);
            /* 商户范围类型 */
            mcMchntInfo.setMchntType(mcMchntInfoBean.getMchntType());
            /* 商户范围号 */
            mcMchntInfo.setMchntNo(mcMchntInfoBean.getMchntNo());
            /* 商户范围名称 */
            mcMchntInfo.setMchntName(mcMchntInfoBean.getMchntName());
            /* 新增商户标志 */
            mcMchntInfo.setNewMchntFlag(CommonConstant.MCHNT_NEW_FLAG);
            /* 退出标志 */
            mcMchntInfo.setExitFlag(CommonConstant.MC_EXIT_FLAG);
            if (!StringUtil.isNullorEmpty(mcMchntInfoBean.getMcNo())) {
                /* 退出标志 */
                mcMchntInfo.setExitFlag(CommonConstant.MC_TRANSFER_FLAG);
            }
            /* 达标日期 */
            mcMchntInfo.setQualifiedDate(DateUtil.getCurrentDate());
            /* 创建人 */
            mcMchntInfo.setOprId(queryId);
            /* 创建时间 */
            mcMchntInfo.setCreateTime(currentTime);

        } catch (Exception e) {
            rglog.error("转换营销活动商户信息异常,异常信息:<{}>", StringUtil.ExceptionToString(e));
            throw new BizException(RespCodeEnum.SYSTEM_ERROR.getRespCode(), RespCodeEnum.SYSTEM_ERROR.getRespDesc());
        }

        return mcMchntInfo;
    }

    /**
     * 营销活动商户信息表数据转换
     * 正式表数据转换为临时表数据(进行中新增商户)
     *
     * @param tmMcMchntInfo 营销活动商户信息表
     * @return cc.rengu.igas.momp.common.entity.TMPlatBizTmp 业务数据临时表
     * @author JL Pang
     * @since 2020/5/28 22:26
     */
    private TMPlatBizTmp convertMcMchntInfoToTmps(TMMcMchntInfo tmMcMchntInfo) {

        TMPlatBizTmp tmPlatBizTmp = new TMPlatBizTmp();

        try {
            /* 法人机构标识 */
            tmPlatBizTmp.setInstId(tmMcMchntInfo.getInstId());
            /* 营销活动编号 */
            tmPlatBizTmp.setMcNo(tmMcMchntInfo.getMcNo());
            //商户名称
            tmPlatBizTmp.setMchntName(tmMcMchntInfo.getMchntName());
            /* 商户编号 */
            tmPlatBizTmp.setMchntNo(tmMcMchntInfo.getMchntNo());
            /* 操作菜单 */
            tmPlatBizTmp.setOprMenuId(CommonConstant.DEFAULT_MENU_ID);
            /* 操作菜单名 */
            tmPlatBizTmp.setOprMenuName(CommonConstant.DEFAULT_MENU_NAME);
            /* 操作表中文名 */
            tmPlatBizTmp.setOprTableCname(TableNameEnum.T_M_MC_MCHNT_INFO.getTableNameDesc());
            /* 操作表英文名 */
            tmPlatBizTmp.setOprTableEname(TableNameEnum.T_M_MC_MCHNT_INFO.getTableNameCode());
            /* 操作 */
            tmPlatBizTmp.setBizOpr(BizOprEnum.INSERT.getBizOprCode());
            /* 新增商户标志 */
            tmMcMchntInfo.setNewMchntFlag(CommonConstant.MCHNT_OLD_FLAG);
            /* 退出标志 */
            tmMcMchntInfo.setExitFlag(CommonConstant.MC_NOT_EXIT_FLAG);
            /* 达标日期 */
            tmMcMchntInfo.setQualifiedDate(DateUtil.getCurrentDate());
            /* 数据域1 - 拼接JSON字符串 */
            tmPlatBizTmp.setBizData1(JsonOperation.packMcMchntInfoTmpBizData1(tmMcMchntInfo, rglog));
            /* 状态 */
            tmPlatBizTmp.setBizStatus(BizStatusEnum.TMP_STATUS.getBizStatusCode());
            /* 营销活动状态 */
            tmPlatBizTmp.setMcStatus(McStatusEnum.DRAFT.getMcStatusCode());
            /* 审批状态 */
            tmPlatBizTmp.setAuditStatus(AuditStatusEnum.MODIFY_AUDIT_PROCESSING.getAuditStatusCode());
            /* 创建人 */
            tmPlatBizTmp.setOprId(queryId);
            /* 创建时间 */
            tmPlatBizTmp.setCreateTime(currentTime);

            return tmPlatBizTmp;

        } catch (Exception e) {
            rglog.error("公共业务数据不可为空!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }
    }

    /*
     *检查商户是否参加其他银行对商户手续费减免活动
     * @author JL Pang
     * @since 2020/9/10 22:26
     *tmMcMchntInfoList 新增商户list
     */
    private void checkMchntStatus(List<TMMcMchntInfo> newtmMcMchntInfoList) throws Exception {
        //查询营销商户表数据
        McMchntInfoMapper mcMchntInfoMapper = new McMchntInfoMapperImpl();
        for (TMMcMchntInfo newtmMcMchntInfo : newtmMcMchntInfoList) {
            List<TMMcMchntInfo> tmMcMchntInfoList = mcMchntInfoMapper.selectMfdMchntInfoListByinstIdMchntNo(instId, newtmMcMchntInfo.getMchntNo());
            if (null != tmMcMchntInfoList && tmMcMchntInfoList.size() > 0) {
                //上送商户是否参加了当前日期内其他进行中的手续费营销活动(new_mchnt_flag为1 exit_flag为1) (new_mchnt_flag为0 exit_flag为0)
                List<TMMcMchntInfo> jxzMchntList = tmMcMchntInfoList.stream().filter(item -> (item.getNewMchntFlag().equals(CommonConstant.MCHNT_OLD_FLAG) && item.getExitFlag().equals(CommonConstant.MC_NOT_EXIT_FLAG)) || (item.getNewMchntFlag().equals(CommonConstant.MCHNT_NEW_FLAG) && item.getExitFlag().equals(CommonConstant.MC_EXIT_FLAG))
                        || (item.getNewMchntFlag().equals(CommonConstant.MCHNT_OLD_FLAG) && item.getExitFlag().equals(CommonConstant.MC_EXIT_FLAG))).collect(Collectors.toList());
                rglog.info("营销活动商户信息表状态为(new_mchnt_flag为1 exit_flag为1)或(new_mchnt_flag为0 exit_flag为0)或(new_mchnt_flag为0 exit_flag为1)的条数<{}>", jxzMchntList.size());
                if (jxzMchntList.size() > 0) {
                    for (TMMcMchntInfo tmMcMchntInfo : jxzMchntList) {
                        /* 营销活动信息正式表查询 */
                        McDetailInfoMapper mcDetailInfoMapper = new McDetailInfoMapperImpl();
                        TMMcMchntFeeDiscountInfo tmMcMchntFeeDiscountInfo = (TMMcMchntFeeDiscountInfo) mcDetailInfoMapper.selectMcDetailInfo(instId, tmMcMchntInfo.getMcNo(), TableNameEnum.T_M_MC_MFD_INFO.getTableNameCode());
                        //如果活动状态不是自然结束，强制结束，草稿；审批状态不是新增拒绝，审批拒绝，发布拒绝的状态，直接抛异常
                        if (null != tmMcMchntFeeDiscountInfo && !tmMcMchntFeeDiscountInfo.getMcStatus().equals(McStatusEnum.ENDED.getMcStatusCode()) && !tmMcMchntFeeDiscountInfo.getMcStatus().equals(McStatusEnum.FORCE_ENDED.getMcStatusCode()) && !tmMcMchntFeeDiscountInfo.getMcStatus().equals(McStatusEnum.ALL_ENDED.getMcStatusCode())
                                && !tmMcMchntFeeDiscountInfo.getAuditStatus().equals(AuditStatusEnum.NEW_AUDIT_REJECT.getAuditStatusCode()) && !tmMcMchntFeeDiscountInfo.getAuditStatus().equals(AuditStatusEnum.MODIFY_AUDIT_REJECT.getAuditStatusCode()) && !tmMcMchntFeeDiscountInfo.getAuditStatus().equals(AuditStatusEnum.PUBLISH_AUDIT_REJECT.getAuditStatusCode())
                                && !tmMcMchntFeeDiscountInfo.getMcStatus().equals(McStatusEnum.DRAFT.getMcStatusCode())) {
                            rglog.error("商户<{}>已参加其他进行中手续费活动<{}>！", tmMcMchntInfo.getMchntNo(), tmMcMchntFeeDiscountInfo.getMcNo());
                            throw new BizException(RespCodeEnum.WRONG_MCHNT_ISALREADY_INMC_ERROR.getRespCode(), RespCodeEnum.WRONG_MCHNT_ISALREADY_INMC_ERROR.getRespDesc() + ",商户<" + tmMcMchntInfo.getMchntNo() + ">");
                        }
                    }
                }
                //上送商户是否有从a活动到b活动的转移(new_mchnt_flag为2 exit_flag为2) (new_mchnt_flag为1 exit_flag为2)
                List<TMMcMchntInfo> zyMchntList = tmMcMchntInfoList.stream().filter(item -> (item.getNewMchntFlag().equals(CommonConstant.MCHNT_REFUSE_FLAG) && item.getExitFlag().equals(CommonConstant.MC_TRANSFER_FLAG)) || (item.getNewMchntFlag().equals(CommonConstant.MCHNT_NEW_FLAG) && item.getExitFlag().equals(CommonConstant.MC_TRANSFER_FLAG))).collect(Collectors.toList());
                rglog.info("营销活动商户信息表状态为(new_mchnt_flag为2 exit_flag为2)或(new_mchnt_flag为1 exit_flag为2)的条数<{}>", zyMchntList.size());
                if (zyMchntList.size() > 0) {
                    for (TMMcMchntInfo tmMcMchntInfo : zyMchntList) {
                        /* 营销活动信息正式表查询 */
                        McDetailInfoMapper mcDetailInfoMapper = new McDetailInfoMapperImpl();
                        TMMcMchntFeeDiscountInfo tmMcMchntFeeDiscountInfo = (TMMcMchntFeeDiscountInfo) mcDetailInfoMapper.selectMcDetailInfo(instId, tmMcMchntInfo.getMcNo(), TableNameEnum.T_M_MC_MFD_INFO.getTableNameCode());
                        if (null != tmMcMchntFeeDiscountInfo && !tmMcMchntFeeDiscountInfo.getMcStatus().equals(McStatusEnum.ENDED.getMcStatusCode()) && !tmMcMchntFeeDiscountInfo.getMcStatus().equals(McStatusEnum.FORCE_ENDED.getMcStatusCode()) && !tmMcMchntFeeDiscountInfo.getMcStatus().equals(McStatusEnum.ALL_ENDED.getMcStatusCode())) {
                            rglog.error("商户<{}>正在进行活动转移！", tmMcMchntInfo.getMchntNo(), tmMcMchntFeeDiscountInfo.getMcNo());
                            throw new BizException(RespCodeEnum.MCHNT_TRANSFER_WRONG.getRespCode(), RespCodeEnum.MCHNT_TRANSFER_WRONG.getRespDesc() + ",商户<" + tmMcMchntInfo.getMchntNo() + ">");
                        }
                    }
                }
            }
        }
    }

    /**
     * 营销活动商户信息表数据转换
     * 临时表数据转换为临时表数据(进行中新增商户)
     *
     * @param tmPlatBizTmp 临时信息表
     * @return cc.rengu.igas.momp.common.entity.TMPlatBizTmp 业务数据临时表
     * @author JL Pang
     * @since 2020/9/11 22:26
     */
    private TMMcMchntInfo convertPlatBizToMchntInfo(TMPlatBizTmp tmPlatBizTmp) {

        TMMcMchntInfo tmMcMchntInfo = new TMMcMchntInfo();

        try {
            /* 法人机构标识 */
            tmMcMchntInfo.setInstId(tmPlatBizTmp.getInstId());
            /* 商户编号 */
            tmMcMchntInfo.setMchntNo(tmPlatBizTmp.getMchntNo());

            return tmMcMchntInfo;

        } catch (Exception e) {
            rglog.error("公共业务数据不可为空!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }
    }

    /**
     * 4.插入新 （正式）营销活动商户信息表 （商户 和 新的营销活动）
     *
     * @param dbsUtil         数据库操作工具类
     * @param mcMchntInfoBean
     * @throws Exception 异常信息
     * @author liujinan
     * @since 2020-09-15 20:22:50
     */
    private void insertTMMcMchntInfo(McMchntInfoBean mcMchntInfoBean, DbsUtil dbsUtil) throws Exception {

        TMMcMchntInfo tmMcMchntInfo = new TMMcMchntInfo();
        tmMcMchntInfo.setInstId(instId);
        tmMcMchntInfo.setMcNo(mcNo);
        tmMcMchntInfo.setMchntNo(mcMchntInfoBean.getMchntNo());
        //商户范围类型
        tmMcMchntInfo.setMchntType(mcMchntInfoBean.getMchntType());
        //商户范围名称
        tmMcMchntInfo.setMchntName(mcMchntInfoBean.getMchntName());
        //新增商户标志 new_mchnt_flag改为1新增，
        tmMcMchntInfo.setNewMchntFlag(CommonConstant.MCHNT_NEW_FLAG);
        //退出标志 exit_flag  2
        tmMcMchntInfo.setExitFlag(CommonConstant.MC_TRANSFER_FLAG);

        tmMcMchntInfo.setOprId(lastOprId);
        tmMcMchntInfo.setCreateTime(currentTime);

        McMchntInfoMapper mcMchntInfoMapper = new McMchntInfoMapperImpl();
        int returnCode = mcMchntInfoMapper.insertTMMcMchntInfo(tmMcMchntInfo);
        if (Database.DBS_SUCCESS != returnCode) {
            /* 异常结束数据库事务 */
            rglog.error("新增营销活动商户信息表信息失败, RETURN_CODE=<{}>, MC_NO=<{}>，MCHNT_NO=<{}>", mcNo, mcMchntInfoBean.getMchntNo());
            dbsUtil.dbsEndTransaction(false);
            throw new BizException(RespCodeEnum.DATABASE_OPR_ERROR.getRespCode(), RespCodeEnum.DATABASE_OPR_ERROR.getRespDesc());
        }
    }

    /**
     * 更新营销活动信息 (营销活动商户信息表) 更换新的营销活动号
     *
     * @param mcMchntInfoBean
     * @throws Exception 异常信息
     * @author liujinan
     * @since 2020-09-15 16:58:51
     */
    private void updateTMMcMchntInfoByMcNo(McMchntInfoBean mcMchntInfoBean, DbsUtil dbsUtil) throws Exception {
        /* 营销活动信息更新 */
        //组装数据
        TMMcMchntInfo tmMcMchntInfo = new TMMcMchntInfo();

        tmMcMchntInfo.setInstId(instId);
        tmMcMchntInfo.setMchntNo(mcMchntInfoBean.getMchntNo());
        tmMcMchntInfo.setMcNo(mcMchntInfoBean.getMcNo());

        tmMcMchntInfo.setRemark1(mcNo);
        tmMcMchntInfo.setLastOprId(lastOprId);
        tmMcMchntInfo.setUpdateTime(currentTime);

        McMchntInfoMapper mcMchntInfoMapper = new McMchntInfoMapperImpl();
        int updateReturnCode = mcMchntInfoMapper.updateTMMcMchntInfoByMcNo(tmMcMchntInfo);
        if (updateReturnCode != 0) {
            rglog.error("根据主键更新营销活动商户信息表T_M_MC_MCHNT_INFO失败！");
            dbsUtil.dbsEndTransaction(false);
            throw new BizException(RespCodeEnum.WRONG_UPDATEMCMCHNT_ERROR.getRespCode(), RespCodeEnum.WRONG_UPDATEMCMCHNT_ERROR.getRespDesc());
        }

    }

    /*
     *检查商户基本信息表商户状态是否为正式商户
     * @author JL Pang
     * @since 2020/11/07 11:26
     *mcMchntInfoBeanList 新增商户list
     */
    private void checkBaseMchntStatus(List<McMchntInfoBean> mcMchntInfoBeanList) throws Exception {
        //查询商户基本信息表数据
        MchntInfoMapper mchntInfoMapper = new MchntInfoMapperImpl();
        for (McMchntInfoBean mcMchntInfoBean : mcMchntInfoBeanList) {
            TBMchntBaseInfo tbMchntBaseInfo = mchntInfoMapper.selectMchntBaseInfoByMchntNo(instId, mcMchntInfoBean.getMchntNo());
            if (null == tbMchntBaseInfo) {
                rglog.error("交易流水号：<{}>, 商户<{}>信息查询异常！", transNo, mcMchntInfoBean.getMchntNo());
                throw new BizException(RespCodeEnum.GET_MCHNT_BASE_INFO_ERROR_BY_FORMAL.getRespCode(), RespCodeEnum.GET_MCHNT_BASE_INFO_ERROR_BY_FORMAL.getRespDesc() + ",商户号" + mcMchntInfoBean.getMchntNo());
            }
        }

    }

    /**
     * 获取营销活动商户信息列表(正式表)
     *
     * @param exitFlag     退出标志
     * @param newMchntFlag 新商户标志
     * @return java.util.List<cc.rengu.igas.momp.common.entity.TMMcMchntInfo> 营销活动商户信息表
     * @throws Exception 异常信息
     * @author liujinan
     * @date 2020/8/10 18:35
     */
    private List<TMMcMchntInfo> getAllMcMchntListInfoByinstIdMcMoFlag(String exitFlag, String newMchntFlag) throws Exception {
        List<Object> tmMcMchntInfoList = new ArrayList<>();
        PageInfo pageInfo = new PageInfo();
        int i = 0;
        pageInfo.setPageIndex(i);
        pageInfo.setPageSize(9999);
        McMchntInfoMapper mcMchntInfoMapper = new McMchntInfoMapperImpl();
        PageResult tmMcMchntInfoPageInfo = mcMchntInfoMapper.selectMcMchntInfoListByinstIdMcMoFlag(instId, mcNo, exitFlag, newMchntFlag, pageInfo);
        if (null != tmMcMchntInfoPageInfo && null != tmMcMchntInfoPageInfo.getResult()) {
            tmMcMchntInfoList.addAll(tmMcMchntInfoPageInfo.getResult());
        }
        while (null != tmMcMchntInfoPageInfo && null != tmMcMchntInfoPageInfo.getResult() && tmMcMchntInfoPageInfo.getResult().size() >= 1) {
            i++;
            pageInfo.setPageIndex(i);
            tmMcMchntInfoPageInfo = mcMchntInfoMapper.selectMcMchntInfoListByinstIdMcMoFlag(instId, mcNo, exitFlag, newMchntFlag, pageInfo);
            if (null != tmMcMchntInfoPageInfo && null != tmMcMchntInfoPageInfo.getResult()) {
                tmMcMchntInfoList.addAll(tmMcMchntInfoPageInfo.getResult());
            }
        }
        List<TMMcMchntInfo> mcMchntInfoList = tmMcMchntInfoList.stream().map(item -> (TMMcMchntInfo) (item)).collect(Collectors.toList());
        return mcMchntInfoList;
    }


    private List<TMPlatBizTmp> getAllMcMchntListInfoByInstIdMcNo() throws Exception {
        List<Object> tmMcMchntInfoList = new ArrayList<>();
        PageInfo pageInfo = new PageInfo();
        int i = 0;
        pageInfo.setPageIndex(i);
        pageInfo.setPageSize(9999);
        PlatBizTmpMapper platBizTmpMapper = new PlatBizTmpMapperImpl();
        PageResult tmMcMchntInfoPageInfo = platBizTmpMapper.selectMchntByInstIdMcNo(instId, mcNo, pageInfo);
        if (null != tmMcMchntInfoPageInfo && null != tmMcMchntInfoPageInfo.getResult()) {
            tmMcMchntInfoList.addAll(tmMcMchntInfoPageInfo.getResult());
        }
        while (null != tmMcMchntInfoPageInfo && null != tmMcMchntInfoPageInfo.getResult() && tmMcMchntInfoPageInfo.getResult().size() >= 1) {
            i++;
            pageInfo.setPageIndex(i);
            tmMcMchntInfoPageInfo = platBizTmpMapper.selectMchntByInstIdMcNo(instId, mcNo, pageInfo);
            if (null != tmMcMchntInfoPageInfo && null != tmMcMchntInfoPageInfo.getResult()) {
                tmMcMchntInfoList.addAll(tmMcMchntInfoPageInfo.getResult());
            }
        }
        List<TMPlatBizTmp> mcMchntInfoList = tmMcMchntInfoList.stream().map(item -> (TMPlatBizTmp) (item)).collect(Collectors.toList());
        return mcMchntInfoList;
    }
}
